package com.es.springbootes.scoll.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.es.springbootes.entity.LivingData;
import com.es.springbootes.scoll.map.ProfileList;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 邹宇杰
 * @email 2901570623@qq.com
 * @site http://zouyujie.top
 * @date 2022/2/10 16:21
 */
@Component
public class JsoupDataService {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    private static final Logger logger = LoggerFactory.getLogger(JsoupDataService.class);


    @Scheduled(cron = "0 */10 * * * ?")
//    @Scheduled(cron = "* * * * * *")
    public void pullData() {
        try{
            RestTemplate restTemplate = new RestTemplate();
            int PageCount = 144;
            //每次更新144页
            for(int i = 0; i < PageCount; i++){
                String livingjsondata = restTemplate.getForObject(
                        "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&tagAll=0&callback=getLiveListJsonpCallback&page="+i,
                        String.class);
                String jsondata = livingjsondata.replaceAll("getLiveListJsonpCallback", "").replaceAll("[\\(\\)]", "");
                JSONObject jsonObject = JSONObject.parseObject(jsondata);
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray datas = data.getJSONArray("datas");
                List<LivingData> dataList = JSONArray.parseArray(datas.toJSONString(), LivingData.class);
                //直播平台的数据进入mysql备份

                //将直播平台的数据全部注入Es中分析
                BulkRequest bulkRequest = new BulkRequest();
                bulkRequest.timeout(TimeValue.timeValueMinutes(2));
                bulkRequest.timeout("2m");
                for (LivingData livingData : dataList) {
                    bulkRequest.add(
                            new IndexRequest("livingdata")
                                    .id(String.valueOf(livingData.getProfileRoom()))
                                    .source(JSONObject.toJSONString(livingData), XContentType.JSON));
                }
                BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                if(bulk.hasFailures()){
                    logger.error(bulk.buildFailureMessage());
                }
                logger.info("第"+i+"页的当前批量任务状态:"+bulk.status().toString());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
